package dyyx.util;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;

import com.aerospike.client.Key;
import com.aerospike.client.Record;
import com.aerospike.client.cluster.Node;

import dyyx.dto.AsClient;
import dyyx.dto.AsNode;
import dyyx.dto.WebResult;

public abstract class AsWebUtil {

	private static final Set<String> OPS = new HashSet<>();

	private static final String OP_CONNECT = "connect";
	private static final String OP_DIS_CONNECT = "disConnect";
	private static final String OP_CLIENT_INFO = "clientInfo";
	private static final String OP_get = "get";
	private static final String OP_head = "head";
	private static final String OP_nodes = "nodes";

	


	static {

		OPS.add(OP_CONNECT);
		OPS.add(OP_DIS_CONNECT);
		OPS.add(OP_CLIENT_INFO);
		OPS.add(OP_get);
		OPS.add(OP_head);
		OPS.add(OP_nodes);

		

		
	}
	
	
	public static void run(HttpServletRequest request, WebResult webResult) throws Exception {
		try {
			run_(request, webResult);
		} catch (Throwable e) {
			webResult.error = true;
			webResult.msg = webResult.op + " error," + e;
		}
	}
	
	
	private static void run_(HttpServletRequest request, WebResult webResult) throws Exception {
		String op = WebUtil.get(request, "op");
		String connstr = WebUtil.get(request, "connstr");
		
		String namespace = WebUtil.get(request, "namespace");
		String dataset = WebUtil.get(request, "dataset");
		String key = WebUtil.get(request, "key");

		if (StringUtils.isBlank(op)) {
			webResult.msg = "op blank";
			return;
		}
		if (!OPS.contains(op)) {
			webResult.msg = "op error";
			webResult.error = true;
			return;
		}
		webResult.op = op;
		

		if (OP_CONNECT.equals(op)) {

			if (StringUtils.isBlank(connstr)) {
				webResult.msg = "connstr blank";
				return;
			}

			AsUtil.connect(connstr);

			webResult.msg = op + " success,connstr=" + connstr;

			return;
		}

		if (OP_DIS_CONNECT.equals(op)) {

			AsUtil.disconnect();

			webResult.msg = op + " success";

			return;
		}

		if (OP_CLIENT_INFO.equals(op)) {

			AsClient asClient = AsUtil.getClient();
			
			
			webResult.msg = asClient.toString();

			return;
		}
		
		if (OP_get.equals(op)) {
			
			if (StringUtils.isBlank(namespace)) {
				webResult.msg = "namespace blank";
				return;
			}
			if (StringUtils.isBlank(dataset)) {
				webResult.msg = "dataset blank";
				return;
			}
			if (StringUtils.isBlank(key)) {
				webResult.msg = "key blank";
				return;
			}
			Key k = new  Key(namespace,dataset,key);

			AsClient asClient = AsUtil.getClient();
			
			Record value = asClient.client.get(null, k);
			String result = null;
			if(value==null) {
				result = "null";
			}else {
				result = FastjsonUtil.toString(value, true);
			}
			
			webResult.msg = result;

			return;
		}
		
	if (OP_head.equals(op)) {
			
			if (StringUtils.isBlank(namespace)) {
				webResult.msg = "namespace blank";
				return;
			}
			if (StringUtils.isBlank(dataset)) {
				webResult.msg = "dataset blank";
				return;
			}
			if (StringUtils.isBlank(key)) {
				webResult.msg = "key blank";
				return;
			}
			Key k = new  Key(namespace,dataset,key);

			AsClient asClient = AsUtil.getClient();
			
			Record value = asClient.client.getHeader(null, k);
			String result = null;
			if(value==null) {
				result = "null";
			}else {
				result = FastjsonUtil.toString(value, true);
			}
			
			webResult.msg = result;

			return;
		}
		
		
	if (OP_nodes.equals(op)) {
		
		if (StringUtils.isBlank(namespace)) {
			webResult.msg = "namespace blank";
			return;
		}
		if (StringUtils.isBlank(dataset)) {
			webResult.msg = "dataset blank";
			return;
		}
		if (StringUtils.isBlank(key)) {
			webResult.msg = "key blank";
			return;
		}

		AsClient asClient = AsUtil.getClient();
		
		Node[] nodes = asClient.client.getNodes();
		List<AsNode> asNodes = buildAsNodes(nodes);
		String result = FastjsonUtil.toString(asNodes, true);
		
		
		webResult.msg = result;

		return;
	}
		
		
	}
	
	
	private static AsNode buildAsNode(Node node) {
		if(node==null) {
			return null;
		}
		AsNode asnode = new AsNode();
		asnode.name = node.getName();
		asnode.host = node.getHost().name;
		asnode.port = node.getHost().port;
		asnode.active = node.isActive();
		
		return asnode;
	}
	
	private static List<AsNode> buildAsNodes(Node[] nodes) {
		if(nodes==null || nodes.length<=0) {
			return null;
		}
		List<AsNode> list = new ArrayList<>();
		for(Node node:nodes) {
			list.add(buildAsNode(node));
		}
		
		return list;
	}
	


}
